/*
 *http://www.apache.org/licenses/LICENSE-2.0
 *Copyright (c) 2021 fullset
 *@ClassName SysUserDaoImpl
 *@date 2021/5/29 16:12
 *@Description TODO
 */
package com.fs.orm.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.fs.orm.dao.SysUserDao;
import com.fs.orm.entity.SysUser;
import com.fs.orm.mapper.SysUserMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserDaoImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserDao {

    @Override
    public boolean addUser(SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        return SqlHelper.retBool(baseMapper.insert(sysUser));
    }

    @Override
    public boolean logicalDeleteById(String id) {
        SysUser sysUser = new SysUser();
        sysUser.setIsDeleted(1);
        UpdateWrapper<SysUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", id);
        int update = baseMapper.update(sysUser, userUpdateWrapper);
        return update > 0 ? true : false;
    }

    @Override
    public boolean batchDel(String ids) {
        String [] arr = ids.split(",");
        UpdateWrapper<SysUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.in("id", arr);
        userUpdateWrapper.set("is_deleted", 1);
        int update = baseMapper.update(null, userUpdateWrapper);
        return update > 0 ? true : false;
    }

    @Override
    public SysUser editUser(SysUser sysUser) {
        UpdateWrapper<SysUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", sysUser.getId());
        userUpdateWrapper.set("user_name", sysUser.getUserName());
        baseMapper.update(sysUser, userUpdateWrapper);
        return sysUser;
    }

    @Override
    public SysUser selectOne(QueryWrapper qWrapper) {
        return baseMapper.selectOne(qWrapper);
    }

    @Override
    public List<SysUser> getList(QueryWrapper qWrapper) {
        return baseMapper.selectList(qWrapper);
    }
}
